package cn.iocoder.yudao.module.crm.service.subject;

import cn.iocoder.yudao.framework.common.enums.Const;
import cn.iocoder.yudao.framework.common.pojo.LayuiTreeNode;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.crm.controller.admin.subject.vo.SubjectListReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.subject.vo.SubjectPageReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.subject.vo.SubjectSaveReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.subject.SubjectDO;
import cn.iocoder.yudao.module.crm.dal.mysql.subject.SubjectMapper;
import cn.iocoder.yudao.module.system.enums.CommonConstants;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.SUBJECT_NOT_EXISTS;

/**
 * 栏目 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class SubjectServiceImpl implements SubjectService {

    @Resource
    private SubjectMapper subjectMapper;

    @Override
    public Long createSubject(SubjectSaveReqVO createReqVO) {
        if(createReqVO.getParentId() != null && createReqVO.getParentId() > 0) {
            SubjectDO parentSubject = subjectMapper.selectById(createReqVO.getParentId());
            createReqVO.setLevel(parentSubject.getLevel() + 1);
            createReqVO.setParentId(createReqVO.getParentId());
        }else{
            createReqVO.setLevel(1);
            createReqVO.setParentId(CommonConstants.ROOT_ID);
        }
        // 插入
        SubjectDO subject = BeanUtils.toBean(createReqVO, SubjectDO.class);
        subjectMapper.insert(subject);
        // 返回
        return subject.getId();
    }

    @Override
    public void updateSubject(SubjectSaveReqVO updateReqVO) {
        // 校验存在
        validateSubjectExists(updateReqVO.getId());
        // 更新
        SubjectDO updateObj = BeanUtils.toBean(updateReqVO, SubjectDO.class);
        subjectMapper.updateById(updateObj);
    }

    @Override
    public void deleteSubject(Long id) {
        // 校验存在
        validateSubjectExists(id);
        // 删除
        subjectMapper.deleteById(id);
    }

    private void validateSubjectExists(Long id) {
        if (subjectMapper.selectById(id) == null) {
            throw exception(SUBJECT_NOT_EXISTS);
        }
    }

    @Override
    public SubjectDO getSubject(Long id) {
        return subjectMapper.selectById(id);
    }

    @Override
    public PageResult<SubjectDO> getSubjectPage(SubjectPageReqVO pageReqVO) {
        return subjectMapper.selectPage(pageReqVO);
    }


    @Override
    public List<SubjectDO> getSubjectList(SubjectListReqVO reqVO) {
        List<SubjectDO> list = subjectMapper.selectList(reqVO);
        list.sort(Comparator.comparing(SubjectDO::getSort));
        return list;
    }

    @Override
    public List<LayuiTreeNode> buildSubjectTree() {
        List<SubjectDO> subjectList = getSubjectList(new SubjectListReqVO());
        subjectList.sort(Comparator.comparing(SubjectDO::getSort));
        // 转换为 LayuiTreeNode 列表
        SubjectDO root = new SubjectDO();
        root.setName("全部");
        root.setStatus(0);
        root.setId(0L);
        subjectList.add(root);
        List<LayuiTreeNode> list = subjectList.stream().map(subject -> {
            LayuiTreeNode tree = createLayuiTreeNode(subject);
            return tree;
        }).collect(Collectors.toCollection(ArrayList::new));
        // 构建树形结构
        return LayuiTreeNode.buildTree(list);
    }

    private LayuiTreeNode createLayuiTreeNode(SubjectDO subject) {
        LayuiTreeNode layuiTree = new LayuiTreeNode();
        layuiTree.setId(String.valueOf(subject.getId()));
        layuiTree.setTitle(subject.getName());
        layuiTree.setDisabled(subject.getStatus() == 1);
        layuiTree.setParentId(String.valueOf(subject.getParentId()));
        layuiTree.setSeq(subject.getSort());
        layuiTree.setField(null);
        layuiTree.setChildren(null);
        layuiTree.setHref(null);
        layuiTree.setSpread(Objects.equals(subject.getId(), 0L) ? true:false);
        return layuiTree;
    }
}